package com.ExcelServer.controller;

import com.ExcelServer.common.Result;
import com.ExcelServer.pojo.entity.BaseTableColumns;
import com.ExcelServer.pojo.form.add.AddBaseTable;
import com.ExcelServer.pojo.form.update.UpdateBaseTableForm;
import com.ExcelServer.service.impl.BaseTableColumnsServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/se/performanceBase/info")
@Tag(name = "业绩基础表格列的curd",description = "业绩基本表格的列名的curd")
public class PerformanceBaseController {

    @Autowired
    BaseTableColumnsServiceImpl baseTableColumnsService;

    @Operation(summary = "获取全部",description = "获取全部")
    @GetMapping("/getList")
    public Result getList() {
        List<BaseTableColumns> list = baseTableColumnsService.list();
        return Result.success(list);
    }

    @Operation(summary = "删除",description = "五个列不允许删除 teacher_id:教师id " +
            "points_criteria_id:业绩规则id  " +"start_time:开始时间  "+"end_time:结束时间  "+
            "is_audit:是否被审核  ")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable String id) {
        BaseTableColumns byId = baseTableColumnsService.getById(id);
        String columnsName = byId.getColumnsName();
        if(columnsName.equals("points_criteria_id")||columnsName.equals("teacher_id")||columnsName.equals("is_audit")||columnsName.equals("start_time")||columnsName.equals("end_time")){
            return  Result.fail("此基础列不允许删除");
        }
        else {
            baseTableColumnsService.removeById(id);
        }
        return Result.success("删除成功");
    }

    @Operation(summary = "修改",description = "数据库列名不支持修改，只支持修改 前端数据表以及默认值")
    @PostMapping("/updateById")
    public Result updateById(@RequestBody UpdateBaseTableForm updateBaseTableForm) {

        BaseTableColumns baseTableColumns = new BaseTableColumns();
        baseTableColumns.setId(updateBaseTableForm.getId());
        baseTableColumns.setFColumnsName(updateBaseTableForm.getFColumnsName());

        BaseTableColumns byId = baseTableColumnsService.getById(updateBaseTableForm.getId());
        if(byId.getColumnsName().equals("is_audit")){
            return Result.fail("供审核使用的字段的默认内容不允许修改。");
        }

        baseTableColumns.setDefaultValue(updateBaseTableForm.getDefaultValue());
        baseTableColumnsService.updateById(baseTableColumns);

        return Result.success("修改成功");
    }

    @Operation(summary = "添加",description = "添加一个基础类型，数据库内代表的列(columnsName) 尽可能不要不要使用中文")
    @PostMapping("/addOne")
    public Result addOne(@RequestBody AddBaseTable addBaseTable) {

        BaseTableColumns baseTableColumns = new BaseTableColumns();
        Integer maxId = baseTableColumnsService.getMaxId();
        String columnsName = "_b"+maxId;
        baseTableColumns.setColumnsName(columnsName);
        baseTableColumns.setFColumnsName(addBaseTable.getFColumnsName());
        baseTableColumns.setDefaultValue(addBaseTable.getDefaultValue());
        baseTableColumnsService.save(baseTableColumns);

        return Result.success("添加成功");
    }


}
